local Globals = require('lua.vm.Globals')
local LuaCompiler = require('lua.vm.compiler.LuaCompiler')
local CfgState = require('lua.cfg.CfgState')
local ByteArrayInputStream = require('lua.io.ByteArrayInputStream')
local FileOutputStream = require('lua.io.FileOutputStream')


local src = [[
goto a
:: c ::
print(3)
goto d
:: a ::
print(1)
goto b
:: d ::
print(4)
goto e
:: b ::
print(2)
goto c
:: e ::
]]

local workDir = 'E:\\lua Project\\'
local bis = ByteArrayInputStream:new(src:toByteArray())
local globals = Globals:new()
globals:installCompiler(LuaCompiler)
local p = globals:loadPrototype(bis, 'test.lua', 't')
bis:close()
local state = CfgState:new(p.code)
local g = state:analyze()
g:optimize()
local fos = FileOutputStream:new(workDir .. 'cfg.dot')
fos:writeString(g:toDotString())
fos:close()
-- dot -Tpng cfg.dot -o out.png